package telas.comum;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import telas.componentes.Botao;
import telas.componentes.Painel;
import java.awt.ComponentOrientation;
import java.awt.event.KeyEvent;

public class TelaDialogoCadastro extends TelaDialogo 
{
	private static final long serialVersionUID = 1L;
	private Painel jContentPane = null;
	private Botao btSalvar = null;
	private Botao btFechar = null;
	private IObjetoSistema objeto = null;  //  @jve:decl-index=0:
	private boolean novo;
	private Painel pnlBotoes = null;
	private Painel pnlBotoesInternos = null;
	
	public TelaDialogoCadastro() throws Exception
	{
		super();
		initialize();
	}
	
	public TelaDialogoCadastro(Frame parent, boolean modal) throws Exception
	{
		super(parent, modal);
		initialize();
	}
	
	/**
	 * @param objeto
	 * @param novo
	 */
	public TelaDialogoCadastro(Frame owner, IObjetoSistema objeto, boolean novo) throws Exception
	{
		super(owner);
		this.objeto = objeto;
		this.novo = novo;
		initialize();
	}
	
	/**
	 * @param objeto
	 * @param novo
	 */
	public TelaDialogoCadastro(IObjetoSistema objeto, boolean novo) throws Exception
	{
		super();
		this.objeto = objeto;
		this.novo = novo;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() throws Exception
	{
		this.setModal(true);
		this.setBounds(new Rectangle(200, 200, 436, 272));
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	public Painel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new Painel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPnlBotoes(), BorderLayout.SOUTH);
		}
		
		return jContentPane;
	}
	
	public Botao getBtSalvar() 
	{
		if (btSalvar == null) 
		{
			btSalvar = new Botao();
			btSalvar.setText("Salvar (F5)");
			btSalvar.setMnemonic(KeyEvent.VK_F5);
			btSalvar.setBounds(new Rectangle(2, 0, 95, 23));
			btSalvar.addActionListener
			(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						try
						{
							salvar();
						}
						catch(Exception ex)
						{
							new ExceptionSistema(ex);
						}
					}
				}
			);
		}
		
		return btSalvar;
	}

	public void salvar() throws Exception
	{
		Method objMetodo = null;
		
		try
		{
			preencheObjeto();
			Object objDados = getObjDados();
			Class<?> objClasse = objDados.getClass();
			
			// Salvar
			objMetodo = objClasse.getMethod("salvar");
			objMetodo.invoke(objDados);
			
			processamentoExtra();
			
			setObjDados((IObjetoSistema)objClasse.newInstance());
		}
		catch(Exception ex)
		{
			throw ex;
		}
		
		if(novo)
		{
			limpaCadastro();
		}
		else
		{
			this.dispose();
		}
	}
	
	public Botao getBtFechar() 
	{
		if (btFechar == null) 
		{
			btFechar = new Botao();
			btFechar.setText("Fechar (F6)");
			btFechar.setBounds(new Rectangle(99, 0, 97, 23));
			btFechar.setMnemonic(KeyEvent.VK_F6);
			btFechar.setComponentOrientation(ComponentOrientation.UNKNOWN);
			
			/* Evento para fechar o diálogo */
			btFechar.addActionListener
			(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						try
						{
							fecharTela();
						}
						catch(Exception ex)
						{
							new ExceptionSistema(ex);
						}
					}
				}
			);
		}
		return btFechar;
	}
	
	/**
	 * 
	 * @throws Exception
	 */
	protected void fecharTela() throws Exception
	{
		if(!isNovo())
		{
			getObjDados().desfazerModificacao();
		}
		
		dispose();
	}
	
	public IObjetoSistema getObjDados() 
	{
		return objeto;
	}

	public void setObjDados(IObjetoSistema objeto) 
	{
		this.objeto = objeto;
	}
	
	/**
	 * This method initializes pnlBotoes	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public Painel getPnlBotoes() 
	{
		if (pnlBotoes == null) 
		{
			pnlBotoes = new Painel();
			pnlBotoes.setLayout(new BorderLayout());
			pnlBotoes.setPreferredSize(new Dimension(1, 28));
			pnlBotoes.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			pnlBotoes.add(getPnlBotoesInternos(), BorderLayout.EAST);
		}
		
		return pnlBotoes;
	}

	/**
	 * This method initializes pnlBotoesInternos	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private Painel getPnlBotoesInternos() 
	{
		if (pnlBotoesInternos == null) 
		{
			pnlBotoesInternos = new Painel();
			pnlBotoesInternos.setLayout(null);
			pnlBotoesInternos.setPreferredSize(new Dimension(200, 24));
			pnlBotoesInternos.add(getBtSalvar(), null);
			pnlBotoesInternos.add(getBtFechar(), null);
		}
		
		return pnlBotoesInternos;
	}
	
	public void preencheObjeto()throws Exception{}
	public void limpaCadastro()throws Exception{}
	public void preencherFormulario()throws Exception{}
	public void processamentoExtra() throws Exception{}

	public boolean isNovo() 
	{
		return novo;
	}

	public void setNovo(boolean novo) 
	{
		this.novo = novo;
	} 
}
